package com.jz.community.moduleshoppingguide.neighbor.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;

import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.barlibrary.ImmersionBar;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.eventbus.AppEvent;
import com.jz.community.basecomm.utils.eventbus.EventConfig;
import com.jz.community.commview.commPagerAdapter.CommonViewPagerAdapter;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.R2;
import com.jz.community.moduleshoppingguide.neighbor.bean.CircleMessageBean;
import com.jz.community.moduleshoppingguide.neighbor.presenter.NeighborMainPresenter;
import com.jz.community.moduleshoppingguide.neighbor.ui.NeighborMainView;
import com.jz.community.moduleshoppingguide.neighbor.ui.fragment.NeighborMineFragment;
import com.jz.community.moduleshoppingguide.neighbor.ui.fragment.NeighborNearFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NeighborMainActivity extends BaseMvpActivity<NeighborMainView.View, NeighborMainPresenter> implements NeighborMainView.View {

    @BindView(R2.id.neighbor_main_back)
    ImageView neighborMainBack;
    @BindView(R2.id.neighbor_main_tab)
    SlidingTabLayout neighborMainTab;
    @BindView(R2.id.neighbor_main_publish)
    ImageView neighborMainPublish;
    @BindView(R2.id.neighbor_main_toolbar)
    Toolbar neighborMainToolbar;
    @BindView(R2.id.neighbor_main_vp)
    ViewPager neighborMainVp;

    private NeighborMineFragment mineFragment;
    private NeighborNearFragment nearFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int getContentView() {
        return R.layout.module_shoppingguide_activity_neighbor_main;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected NeighborMainPresenter createPresenter() {
        return new NeighborMainPresenter(this);
    }

    /**
     * @param message
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void EventBus(AppEvent message) {
        if (message.tag == EventConfig.TAB_MOVE_TO_NEAR) {
            //邻里圈无数据时点击空view跳转到附近页面
            neighborMainVp.setCurrentItem(1, false);
        }
    }

    @Override
    protected void initView() {
        super.initView();
        ImmersionBar
                .with(this)
                .statusBarDarkFont(true, 0.2f)//当白色背景状态栏遇到不能改变状态栏字体为深色的设备时，解决方案
                .init();
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        mPresenter.initUserNeighbor();
    }


    private void initViewPager(CircleMessageBean entity) {
        String[] titles = {
                "邻里圈",
                "附近"};

        CommonViewPagerAdapter viewPagerAdapter = new CommonViewPagerAdapter(getSupportFragmentManager());

        // 我的邻里圈
        mineFragment = new NeighborMineFragment();
        Bundle bundle = new Bundle();
        if (entity != null) {
            bundle.putString("circleId", entity.getId());
        } else {
            bundle.putString("circleId", "");
        }
        mineFragment.setArguments(bundle);
        viewPagerAdapter.addFragment(mineFragment);
        // 附近
        nearFragment = new NeighborNearFragment();
        viewPagerAdapter.addFragment(nearFragment);

        neighborMainVp.setOffscreenPageLimit(2);
        neighborMainVp.setAdapter(viewPagerAdapter);

        neighborMainTab.setViewPager(neighborMainVp, titles);

    }


    @Override
    public void isAddCircle(CircleMessageBean entity) {
        //已经加入圈子
        BaseSpUtils.getInstance().setCircleStatus(mActivity, 1);
        initViewPager(entity);
        neighborMainVp.setCurrentItem(0);

    }

    public void jumpNeighborNear() {
        neighborMainVp.setCurrentItem(1);
    }

    @Override
    public void noAddCircle() {
        //用户未加入圈子
        BaseSpUtils.getInstance().setCircleStatus(mActivity, 0);
        initViewPager(null);
        neighborMainVp.setCurrentItem(1, false);

    }

    @Override
    public void showError(String s) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick(R2.id.neighbor_main_back)
    public void backClick() {
        finish();
    }

    @OnClick(R2.id.neighbor_main_publish)
    public void publishClick() {
        int hasCircle = BaseSpUtils.getInstance().getCircleStatus(mActivity);
        if (hasCircle == 1) {
            Intent intent = new Intent(mActivity, PublishMessageActivity.class);
            startActivity(intent);
        } else {
            showNormal("请先加入一个邻里圈，才能发布");
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        nearFragment.onActivityResult(requestCode, resultCode, data);
    }
}
